﻿var _this = this;

var currentBtnType={
		NONE:0,
		ADD:1,
		DELETE:2,
		MODIFY:3
};

function currentBtn(type){
	if(typeof type == 'undefined'){
		return _this.btnType;
	}else{
		_this.btnType=type;
	}
}

$(document).ready(function() {
	
	var lang = getLanguageResource();
	$('#tbl2 tbody tr').click(selectTbl2Tr);
	$('#add_code').click(add_code);
	$('#remove_code').click(remove_code);
	$('#modify_code').click(modify_code);
	$('#save_position').click(save_position);

//	console.log(currentBtn());
	
});

function getLastChildren(){
	return $('#tbl2').find('#add_position').prev();
}

function add_code(){
	currentBtn(currentBtnType.ADD);	// 현재 타입 저장.
	
	var last = getLastChildren();
	var number = last.find('#position_code').text();
	number=number*1;
	number = formatter.threeDigits(++number);
	$('#input_position_code').val(number);
//	$('#tbl2').find('#add_position').addClass("table_clicked");
	tableClickedCSS('tbl2', $('#tbl2').find('#add_position'));
	
	$('#add_position').show();
	showCodeManagement('');
	$('#tbl2').find('#add_position').find('#input_position_code').focus();
	$('#tbl3').find('#input_position_code_name').focus();
	
}

function save_position(){
	var message="";
	var code = "";
	var name = "";
	var memo = "";
	var nSeq = "";
	var title ="";
	var param ={};
	var successMsg = "";
	var lBtn="";
	var rBtn="취소";
	
	var action_url = "";
	var type = currentBtn();
	
	
	currentBtnType={
			NONE:0,
			ADD:1,
			DELETE:2,
			MODIFY:3
	};
	
	switch(type){
	case currentBtnType.ADD :{
		action_url='action/add_position.json';
		try{
			code = $('#tbl3').find('#position_code').val();
			name = $('#tbl3').find('#position_name').val();
			memo = $('#tbl3').find('#memo').val();
			title = "아래 내용을 확인해 주세요.";
			successMsg = "정상적으로 등록 되었습니다.";
			lBtn = "등록";
			
			message = "직책 코드 : "+code+"\n"+
			"직책 코드명 : "+name+"\n"+
			"설명 : "+memo;
			
			param={'position_name':name,
					'position_code':code,
					'memo':memo
					};
		}catch(e){
			
		}
		if(message.length<1) {
			// error
			return;
		}
		break;
	}
	
	case currentBtnType.MODIFY :{
		action_url='action/modify_position.json';
		
		
		try{
			var selectedTr = $('#tbl2').find('.table_clicked');
			
			
			if(!selectedTr.find('#nSeq').text()){
				pezAlert("선택된 코드가 없습니다.");
				return;
			}
			nSeq = selectedTr.find('#nSeq').text();
			code = $('#tbl3').find('#position_code').val();
			name = $('#tbl3').find('#position_name').val();
			memo = $('#tbl3').find('#memo').val();
			 
			title = "아래 내용을 확인해 주세요.";
			successMsg = "정상적으로 변경 되었습니다.";
			lBtn = "변경";
			
			message = "직책 코드 : "+code+"\n"+
			"직책 코드명 : "+name+"\n"+
			"설명 : "+memo;
			
			param={
					'nSeq':nSeq,
					'position_name':name,
					'position_code':code,
					'memo':memo
					};
		}catch(e){
			
		}
		
		break;
	}
	
	default:{
		return;
	}
	}
	
	if(message.length<1) {
		// error
		return;
	}
	
	pezAlert(message, function(){
		try{
			$.post(action_url, param, function(data) {
//				
				pezAlert(successMsg, function(){
					location.reload();	
				}, function(){
					location.reload();
				}).show();
				
			}).error(function() { pezAlert("error"); });
		}catch(e){
			errLog(e);
		}
	}, function(){
		return;
	}).type(alertType.CONFIRM)
	.title(title)
	.lButton(lBtn)
	.rButton(rBtn).show();
	
}

function doMemo(id, param){
	var selectedTr = $('#tbl2').find('.table_clicked');
	if(selectedTr.attr('id')!='add_position') return;
	
	var right = $('#tbl2').find('#add_position').find('#memo');
	
	right.val($(id).val());
}

function doRight1(id, param){
	var left = $('#tbl2').find('#add_position').find('#input_position_code');
	left.val($(id).val());
}

function doRight2(id, param){
	var left = $('#tbl2').find('#add_position').find('#input_position_name');
//	console.log($(id).val());
	left.val($(id).val());
//	console.log(left.val());
}

function doLengthLimit(id, param){
	var limit=param;
	var length = $(id).val().length;
	if(length>limit){
		$(id).val($(id).val().substring(0,limit));
	}
};


function remove_code(){
	
	var selectedTr = $('#tbl2').find('.table_clicked');
	
	if(!selectedTr.find('#nSeq').text()){
		pezAlert("선택된 코드가 없습니다.");
		return;
	}
	currentBtn(currentBtnType.DELETE);	// 현재 타입 저장.
	var message="";
	var code = "";
	var name = "";
	var memo = "";
	var param ={};
	try{
		code = $('#tbl3').find('#position_code').val();
		name = $('#tbl3').find('#position_name').val();
		memo = $('#tbl3').find('#memo').val();
		message = "삭제후 복원이 불가합니다.\n잘 확인해 주세요.\n\n"+ 
			"직책 코드 : "+code+"\n"+
		"직책 코드명 : "+name+"\n"+
		"설명 : "+memo;
		
		param={'nSeq':selectedTr.find('#nSeq').text()};
	}catch(e){
		
	}
	
	pezAlert(message, function(){
		try{
			$.post('action/remove_position.json', param, function(data) {
//				
				pezAlert("정상적으로 변경 되었습니다.", function(){
					location.reload();	
				}, function(){
					location.reload();
				}).show();
				
			}).error(function() { pezAlert("error"); });
		}catch(e){
			errLog(e);
		}
	}, function(){
		return;
	}).type(alertType.CONFIRM)
	.title("삭제 합니까?")
	.lButton("삭제")
	.rButton("취소").show();
}
function modify_code(){
	var selectedTr = $('#tbl2').find('.table_clicked');
	
	if(!selectedTr.find('#nSeq').text()){
		pezAlert("선택된 코드가 없습니다.");
		return;
	}
	currentBtn(currentBtnType.MODIFY);	// 현재 타입 저장.
	var targetTr = $('#tbl3');
	spyOnKey(targetTr.find('#position_code'), doLengthLimit, 3);
	spyOnKey(targetTr.find('#position_name'), doLengthLimit, 10);
	spyOnKey(targetTr.find('#memo'), doLengthLimit, 100);
	readOnly(false);
	$('#btn_save').show();
	$('#btn_treeMenu').hide();
}

function selectTbl2Tr(){
	tableClickedCSS('tbl2', this);
	showCodeManagement($(this).find('#nSeq').text());
}

function tableClickedCSS(tableId, clickedTr){
	$('#'+tableId+' tbody tr').each(function() {
		$(this).removeClass("table_clicked");
	});
	$(clickedTr).addClass("table_clicked");
}

function showCodeManagement(nSeq){
	log("showCodeManagement nSeq = "+nSeq);
	var selectedTr = $('#tbl2').find('.table_clicked');
	var targetTr = $('#tbl3');
	if(nSeq==""){
//		
		spyOnKey(targetTr.find('#position_code'), doLengthLimit, 3);
		spyOnKey(targetTr.find('#position_code'), doRight1);
		spyOnKey(targetTr.find('#position_name'), doLengthLimit, 10);
		spyOnKey(targetTr.find('#position_name'), doRight2);
		spyOnKey(targetTr.find('#memo'), doLengthLimit, 100);
		spyOnKey(targetTr.find('#memo'), doMemo);
		
		readOnly(false);
		targetTr.find('#position_code').val(selectedTr.find('#input_position_code').val());
		targetTr.find('#position_name').val(selectedTr.find('#input_position_name').val());
		targetTr.find('#memo').val(selectedTr.find('#memo').val());
		$('#btn_save').show();
		$('#btn_treeMenu').hide();
	}else{
		
		readOnly(true);
		
		targetTr.find('#position_code').val(selectedTr.find('#position_code').text());
		targetTr.find('#position_name').val(selectedTr.find('#position_name').text());
		targetTr.find('#memo').val(selectedTr.find('#memo').text());
		
//		
		$('#btn_treeMenu').show();
		$('#btn_save').hide();
	}
}

function readOnly(flag){
	
	if(typeof flag!='boolean'){
		return;
//		console.log("return");
	}
//	console.log("readOnly");
	
	var targetTr = $('#tbl3');
	targetTr.find('#position_code').attr('readonly', flag);
	targetTr.find('#position_name').attr('readonly', flag);
	targetTr.find('#memo').attr('readonly', flag);
	if(flag){
		targetTr.find('#position_code').addClass('background_gray');
		targetTr.find('#position_name').addClass('background_gray');
		targetTr.find('#memo').addClass('background_gray');
	}else{
		targetTr.find('#position_code').removeClass('background_gray');
		targetTr.find('#position_name').removeClass('background_gray');
		targetTr.find('#memo').removeClass('background_gray');
	}
}

function nothing(id, param){
	
}
spyOnKey = function (id, func, param){
	$(id).keyup(function(){
		func(id,  param);
	});
	$(id).keydown(function(){
		func(id,  param);
	});
	$(id).keypress(function(){
		func(id,  param);
	});
	$(id).blur(function(){
		func(id,  param);
	});
	$(id).focusout(function(){
		func(id,  param);
	});
	$(id).focusin(function(){
		func(id,  param);
	});
};


function errLog(e){
//	console.log(e);
}

function log(e){
	errLog("======== log start");
	errLog(e);
	errLog("======== log the end");
}